package com.yz.food.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yz.food.cms.dao.FoodMapper;
import com.yz.food.cms.pojo.Food;
import com.yz.food.cms.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodMapper foodMapper;

    /***
     * 查询所有食材
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Food> getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Food> foods = foodMapper.getAll();
        PageInfo<Food> pageInfo = new PageInfo<>(foods);
        return pageInfo;
    }

    /**
     * 更新食材
     * @param food
     * @return
     */
    @Override
    public int updateFood(Food food) {
        if (food.getId() == null) {
            Food newFood = new Food();
            newFood.setCategoryId(food.getCategoryId());
            newFood.setDetail(food.getDetail());
            newFood.setFoodCondition(food.getFoodCondition());
            newFood.setFoodLong(food.getFoodLong());
            newFood.setFoodName(food.getFoodName());
            newFood.setFoodStatus(food.getFoodStatus());
            newFood.setGuarantee(food.getGuarantee());
            newFood.setHight(food.getHight());
            newFood.setImgUrl(food.getImgUrl());
            newFood.setIsHot(food.getIsHot());
            newFood.setWeight(food.getWeight());
            newFood.setIntroduce(food.getIntroduce());
            newFood.setManufacture(food.getManufacture());
            newFood.setWidth(food.getWidth());
            newFood.setQuantity(food.getQuantity());
            newFood.setNowPrice(food.getNowPrice());
            newFood.setOldPrice(food.getOldPrice());
            newFood.setPlace(food.getPlace());
            return foodMapper.insertSelective(newFood);
        }
        Food newFood = new Food();
        newFood.setId(food.getId());
        newFood.setCategoryId(food.getCategoryId());
        newFood.setDetail(food.getDetail());
        newFood.setFoodCondition(food.getFoodCondition());
        newFood.setFoodLong(food.getFoodLong());
        newFood.setFoodName(food.getFoodName());
        newFood.setFoodStatus(food.getFoodStatus());
        newFood.setGuarantee(food.getGuarantee());
        newFood.setHight(food.getHight());
        newFood.setImgUrl(food.getImgUrl());
        newFood.setIsHot(food.getIsHot());
        newFood.setWeight(food.getWeight());
        newFood.setIntroduce(food.getIntroduce());
        newFood.setManufacture(food.getManufacture());
        newFood.setWidth(food.getWidth());
        newFood.setQuantity(food.getQuantity());
        newFood.setNowPrice(food.getNowPrice());
        newFood.setOldPrice(food.getOldPrice());
        newFood.setPlace(food.getPlace());
        return foodMapper.updateByPrimaryKeySelective(newFood);
    }
}
